package com.yyg.wiki.service;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyg.wiki.domain.User;
import com.yyg.wiki.domain.UserExample;
import com.yyg.wiki.exception.BusinessException;
import com.yyg.wiki.exception.BusinessExceptionCode;
import com.yyg.wiki.mapper.UserMapper;
import com.yyg.wiki.req.UserLoginReq;
import com.yyg.wiki.req.UserQueryReq;
import com.yyg.wiki.req.UserResetPasswordReq;
import com.yyg.wiki.req.UserSaveReq;
import com.yyg.wiki.resp.PageResp;
import com.yyg.wiki.resp.UserLoginResp;
import com.yyg.wiki.resp.UserQueryResp;
import com.yyg.wiki.util.CopyUtil;
import com.yyg.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: UserService
 * Description:
 * date: 2021/7/22 15:00
 *
 * @author 17878
 * @since JDK 1.8
 */
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private SnowFlake snowFlake;
    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);


    public PageResp<UserQueryResp> list(UserQueryReq Req){

        PageHelper.startPage(Req.getPage(),Req.getSize());
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        if (!ObjectUtils.isEmpty(Req.getLoginName())){
            criteria.andNameLike("%"+Req.getLoginName()+"%");
        }

        List<User> userList = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());
//        List<UserResp> respList=new ArrayList<>();
//        for (User user : userList) {
//
//            UserResp userResp = CopyUtil.copy(user, UserResp.class);
//            respList.add(userResp);
//        }
        List<UserQueryResp> list = CopyUtil.copyList(userList, UserQueryResp.class);
        PageResp<UserQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     *
     * 功能描述:新增操作
     *
     * @param:
     * @return:
     * @auther: 17878
     * @date: 2021/7/29 15:14
     */
    public void save(UserSaveReq req){
        System.out.println("genggaikkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"+req);
        User user=CopyUtil.copy(req,User.class);
        if (ObjectUtils.isEmpty(req.getId())){
            if (ObjectUtils.isEmpty(selectByLoginName(req.getLoginName()))){
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            }else {
                //用户名已存在
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }

        }else {
            user.setLoginName(null);
            user.setPassword(null);
            userMapper.updateByPrimaryKeySelective(user);
        }
    }

    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    public User selectByLoginName(String LoginName){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(LoginName);
        List<User> userList = userMapper.selectByExample(userExample);
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    /**
     *
     * 功能描述:修改密码
     *
     * @param:
     * @return:
     * @auther: 17878
     * @date: 2021/7/29 15:14
     */
    public void resetPassword(UserResetPasswordReq req){
        User user=CopyUtil.copy(req,User.class);
        userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     *
     * 功能描述:登录
     *
     * @param:
     * @return:
     * @auther: 17878
     * @date: 2021/7/29 15:14
     */
    public UserLoginResp login(UserLoginReq req){
        User userDb = selectByLoginName(req.getLoginName());
        if (ObjectUtils.isEmpty(userDb)){
            //用户名不存在
            //抛出异常写法
            LOG.info("用户名不存在, {}", req.getLoginName());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            if (userDb.getPassword().equals(req.getPassword())){
                //登陆成功
                UserLoginResp userLoginResp=CopyUtil.copy(userDb,UserLoginResp.class);
                return userLoginResp;
            }else{
                //密码不对
                LOG.info("密码不对, 输入密码：{}, 数据库密码：{}", req.getPassword(), userDb.getPassword());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }
}
